from pandas import read_csv
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
import pandas as pd
import numpy
from sklearn.preprocessing import StandardScaler
from matplotlib import pyplot as plt


#读列名
def getFeaturesIndex(filename,type='T'):
    features = []
    for i in read_csv(filename).keys():
        features.append(i)
    if type == 'T':
        features.pop()
    else:
        del (features[0])
    print(features)
    return features


#异常值清洗
def box_plot_outliers(data):
    Q3 = numpy.quantile(data,0.75)
    Q1 =numpy.quantile(data,0.25)
    IQR = Q3 - Q1  #Q2
    top = Q3 + 1.5*IQR
    bot = Q1 - 1.5*IQR
    for i in range(len(data)):
        if data[i] > top:
            data[i] = top
        elif data[i] < bot:
            data[i] = top
    return data

def load_dataset(filename):
    dataset = read_csv(filename).values
    return dataset

def fixData(Type='Train'):
    fileName = input('请输入数据集（如train_dataset.csv）：')
    dataset = load_dataset(fileName)
    if Type == 'Train':
        print("正在清洗训练集...")
        FeaturesIndex = getFeaturesIndex(fileName)
        X = dataset[:,0:13]
    else:
        print("正在清洗测试集...")
        FeaturesIndex = getFeaturesIndex(fileName,type=Type)
        X = dataset[:,1:14]


    plt.boxplot(x=X, labels=FeaturesIndex, vert=False,
                flierprops={'markerfacecolor': 'red', 'markeredgecolor': 'blue', 'markersize': 1})
    plt.show()

    for i in range(len(FeaturesIndex)):
        X[:, i] = box_plot_outliers(X[:, i])
    if Type == 'Train':
        print("训练集清洗完成")
    else:
        print("测试集清洗完成")
    # 箱线图
    plt.boxplot(x=X, labels=FeaturesIndex, vert=False,
                flierprops={'markerfacecolor': 'red', 'markeredgecolor': 'red', 'markersize': 1})
    plt.show()
    return X






